﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Vapor.Collections;
using System.Collections.Generic;

namespace GameControlTest
{
    public class GameObject
    {
        private Rect _collisionRect;
        private CompositeTransform _transform;
        private SolidColorBrush _brush;
        private List<GameObject> _collidedWith;

        public Rectangle DrawRectangle { get; private set; }
        public Point Location { get { return new Point(_collisionRect.X, _collisionRect.Y); } }
        public Rect CollisionRect { get { return _collisionRect; } }
        public Color Color { get { return _brush.Color; } }

        public GameObject(Rect r, Color c)
        {
            _collisionRect = r;

            _transform = new CompositeTransform();
            _transform.TranslateX = r.X;
            _transform.TranslateY = r.Y;
            _brush = new SolidColorBrush(c);
            DrawRectangle = new Rectangle();
            DrawRectangle.RenderTransform = _transform;
            DrawRectangle.Fill = _brush;
            DrawRectangle.Width = App.RectSize;
            DrawRectangle.Height = App.RectSize;

            _collidedWith = new List<GameObject>();
        }

        public void Update(double x, double y, float elapsedTimeRatio)
        {
            Translate(x * elapsedTimeRatio, y * elapsedTimeRatio);
            _brush.Color = Colors.Black;

            if (_collidedWith.Count > 0) _brush.Color = Colors.Red;
            _collidedWith.Clear();
        }

        /// <summary>
        /// Must be called from UI thread!
        /// </summary>
        /// <param name="p">Amount to translate by.</param>
        public void Translate(Point p)
        {
            _collisionRect.X += p.X;
            _collisionRect.Y += p.Y;
            _transform.TranslateX += p.X;
            _transform.TranslateY += p.Y;
        }

        /// <summary>
        /// Must be called from UI thread!
        /// </summary>
        /// <param name="p">Amount to translate by.</param>
        public void Translate(double x, double y)
        {
            _collisionRect.X += x;
            _collisionRect.Y += y;
            _transform.TranslateX += x;
            _transform.TranslateY += y;
        }

        /// <summary>
        /// Must be called from UI thread!
        /// </summary>
        /// <param name="go">GameObject with which collision occured.</param>
        public void Collided(GameObject go)
        {
            _collidedWith.Add(go);
        }
    }
}
